package com.sos.model.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 通知表实体
 */
@Data
@Entity
@Table(name = "notification")
@DynamicInsert
@DynamicUpdate
public class Notification implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 通知ID（自增主键）
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 接收用户ID（关联user表，非空）
     */
    @Column(name = "user_id", nullable = false)
    private Long userId;

    /**
     * 关联求助ID（可为null，如系统通知无关联求助）
     */
    @Column(name = "sos_id")
    private Long sosId;

    /**
     * 通知标题（非空）
     */
    @Column(nullable = false, length = 100)
    private String title;

    /**
     * 通知内容（非空）
     */
    @Column(nullable = false, length = 500)
    private String content;

    /**
     * 是否已读（0:未读,1:已读，默认0）
     */
    @Column(name = "is_read", nullable = false, columnDefinition = "tinyint default 0")
    private Integer isRead = 0;

    /**
     * 创建时间（数据库默认CURRENT_TIMESTAMP，非空）
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 关联接收用户（多对一）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", insertable = false, updatable = false)
    @JsonIgnore
    private User receiver;

    /**
     * 关联SOS求助（多对一，可为null）
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "sos_id", insertable = false, updatable = false)
    @JsonIgnore
    private SosRequest sosRequest;

    @PrePersist
    public void prePersist() {
        this.createdTime = LocalDateTime.now();
    }
}